package com.stage.calculator;

import com.stage.common.MathUtil;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.sql.Connection;
import java.util.*;

/**
 * Created by fadinglan on 2017/4/18.
 */
public class ProcessSleep {

    private static int win = 10;         //默认传输数据间隔为1/win s

    /**
     * 分离心率体动信号
     * @return
     */
    public static Map<String, List<Double>> SeparteRate(String[] sleepData){
        List<Double> rateData = new ArrayList<>();
        List<Double> moveData = new ArrayList<>();
        Map<String, List<Double>> data = new HashMap<>();
        Double[] range = new Double[win];
//		System.out.println("before applay data length is " + sleepData.length);

        for(int i = 0; i < sleepData.length - win + 1; i += win){

            boolean check = false;

            for (int k = 0; k < win; k ++){
                double value = Double.parseDouble(sleepData[k + i]);

                if (value > 0.0) {
                    rateData.add(value);
                    moveData.add(0.0);
                    check = true;
                }

                if (k == win - 1 && !check){
                    rateData.add(60.0);
                    moveData.add(1.0);
                }

                if (check) break;

            }



        }

        data.put("rate",rateData);
        data.put("move",moveData);
//		System.out.println("after applay rateData length is : " + rateData.size());
        return data;


    }

    /**
     * 中值滤波：对心率数据进行中值滤波，实行标准化
     * @param hseq
     *
     * @return
     */

    public static List<Double> normSleepData(List<Double> hseq){

        double maxHeart = Collections.max(hseq);
        double minHeart = Collections.min(hseq);
        double sub = maxHeart - minHeart;

       List<Double> normHeart = new ArrayList<>();

        for (double d: hseq){
            normHeart.add((d - minHeart) / sub);
        }
        return normHeart;
    }


    /**
     * 标准化：将标准化的心率数据阶梯化为1-51
     * @param normRate
     *
     * @return
     */
    public static List<Integer> stairHeart(List<Double> normRate){

        List<Integer> stairHeart = new ArrayList<>();
        for (double d : normRate){
            stairHeart.add((int)(Math.floor(d/0.02) + 1));

        }

        return stairHeart;

    }

    public void setWin (int win) {
        this.win = win;
    }




}

